package com.example.hrmsaas.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hrmsaas.system.entity.Dept;
import com.example.hrmsaas.system.mapper.DeptMapper;
import com.example.hrmsaas.system.service.IDeptService;
import com.example.hrmsaas.system.utils.DataGridView;
import com.example.hrmsaas.system.utils.vo.DeptVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 雷哥
 * @since 2021-07-16
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {
    private Log log = (Log) LogFactory.getLog(DeptServiceImpl.class);
    @Autowired
    DeptMapper deptMapper;
    @Transactional(readOnly = true)
    @Override
    public DataGridView getDeptAll(DeptVo deptVo) {
        QueryWrapper<Dept> models = new QueryWrapper<>();
        if (null != models) {
            //组装查询条件
            models.like(StringUtils.isNotBlank(deptVo.getTitle()), "title", deptVo.getTitle());
            models.eq(StringUtils.isNotBlank(deptVo.getRemark()), "remark", deptVo.getRemark());
            models.like(StringUtils.isNotBlank(deptVo.getAddress()), "address", deptVo.getAddress());
            models.eq(deptVo.getId()!=null, "id", deptVo.getId()).or().eq(deptVo.getId()!=null, "pid", deptVo.getId());
        } else {
            log.info("DeptVo为空");
        }
        models.orderByDesc("ordernum");
        Page<Dept> page = new Page<>(deptVo.getPage(), deptVo.getLimit());
        this.deptMapper.selectPage(page, models);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    @Override
    public List<Dept> queryAllDeptForList(DeptVo deptVo) {
        QueryWrapper<Dept> deptModel = new QueryWrapper<>();
        deptModel.eq( deptVo.getAvailable()!=null, "available", deptVo.getAvailable());
        return deptMapper.selectList(deptModel);
    }

    @Override
    public Dept addDept(Dept dept) {
        deptMapper.insert(dept);
        return dept;
    }

    @Override
    public Integer queryDeptMaxOrderNum() {
        QueryWrapper<Dept> queryWrapperModel = new QueryWrapper<>();
        Dept dept = deptMapper.selectOne( queryWrapperModel.orderByDesc("ordernum").last("limit 1"));
        return dept.getOrdernum();
    }

    @Override
    public Integer queryDeptCountByPid(Integer id) {
        return this.getBaseMapper().queryDeptCountByPid(id);
    }

    @Override
    public Dept updateDept(Dept dept) {
        this.getBaseMapper().updateById(dept);
        return dept;
    }

    @Override
    public List<Serializable> getDeptChirenIds(Integer id) {
        QueryWrapper<Dept> deptModel = new QueryWrapper<>();
        deptModel.eq( id!=null, "pid", id);
        List<Serializable > Ids = new ArrayList<Serializable>();
        List<Dept> list = this.baseMapper.selectList(deptModel);
        if(list != null){
            for(Dept dept:list){
                assert false;
                Ids.add(String.valueOf(dept.getId()));
            }
        }

        return Ids;
    }



    /**
     * 重写父类方法，以便切面编程更新缓存
     * @param id
     * @return
     */
    @Override
    public Dept getById(Serializable id) {
        return super.getById(id);
    }


    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }
}
